﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Pavilion.GameTools
{
    public class SpeedManipulationTool : GameToolBase
    {
        private GameItem gameItem;
        private SpeedIncrementation currentIncrementation, nextIncrementation;
        private bool isRunning;
        private Logger logger;

        public override void Initialize(GameItem gameItem)
        {
            this.gameItem = gameItem;
            logger = Logger.CreateLogger(gameItem.Name);

            DataExecutionDictionary.Add("IncrementAmount", (value) =>
            {
                logger.WriteLine("  IncrementAmount=" + value);
                string[] speedValues = (value as string).Split(',');

                nextIncrementation.IncrementAmount = new Vector2(float.Parse(speedValues[0]), float.Parse(speedValues[1]));
            });

            DataExecutionDictionary.Add("Limit", (value) =>
            {
                logger.WriteLine("  Limit=" + value);
                string[] speedValues = (value as string).Split(',');
                nextIncrementation.Limit = new Vector2(float.Parse(speedValues[0]), float.Parse(speedValues[1]));
            });
        }

        public override void BeginDataUpdate()
        {
            logger.WriteLine("BeginDataUpdate");
            nextIncrementation = new SpeedIncrementation(this.gameItem);
        }

        public override void EndDataUpdate()
        {
            logger.WriteLine("EndDataUpdate");
            if (isRunning)
                currentIncrementation.StopIncrementing();
            else
                ExecuteNextIncrementation();
        }

        private void ExecuteNextIncrementation()
        {
            if (nextIncrementation == null)
                return;

            logger.WriteLine("ExecuteNextIncrementation");
            isRunning = true;
            currentIncrementation = nextIncrementation;
            nextIncrementation = null;

            currentIncrementation.Complete += new Action<SpeedIncrementation>(SpeedIncrementation_Complete);
            currentIncrementation.StartIncrementing();
        }

        private void SpeedIncrementation_Complete(SpeedIncrementation incrementor)
        {
            isRunning = false;
            incrementor.Complete -= new Action<SpeedIncrementation>(SpeedIncrementation_Complete);
            ExecuteNextIncrementation();
        }

        public override void Deserialize(System.Xml.Linq.XElement xmlProperties) { }
        public override void Serialize() { }
    }

    public struct SpeedManipulationData : IGameDataInfo
    {
        public string ToolName { get { return typeof(SpeedManipulationTool).Name; } }
        public Dictionary<string, GamePropertyInfo> DataConversionDictionary { get { return (dataConversionDictionary == null) ? LoadDataConversion() : dataConversionDictionary; } }
        public IEnumerable<string> CompletePropertyList { get { return DataConversionDictionary.Keys; } }

        private static Dictionary<string, GamePropertyInfo> dataConversionDictionary;

        public GamePropertyInfo ObtainPropertyInfo(string propertyName)
        {
            return DataConversionDictionary[propertyName];
        }

        private static Dictionary<string, GamePropertyInfo> LoadDataConversion()
        {
            dataConversionDictionary = new Dictionary<string, GamePropertyInfo>();
            GamePropertyInfo propertyInfo = new GamePropertyInfo((x) => x, "0,0");

            dataConversionDictionary.Add("IncrementAmount", propertyInfo);
            dataConversionDictionary.Add("Limit", propertyInfo);

            return dataConversionDictionary;
        }
    }
}
